import { Route, Control } from 'react-keeper'
import net from 'clientConfig/util/net';
import urllist from 'clientConfig/urllist';
const { Toast } = SaltUI;
import { monStorage, Storage } from 'clientConfig/util/StoreData';
const PageLogin = require('./subpagecontent/login');
const PageUserbind = require('./subpagecontent/login/userbind');
const PageFillInfo = require('./subpagecontent/login/subpagecontent/fillinfo');
const PageSelectInfo = require('./subpagecontent/login/subpagecontent/selectinfo');
const PageSelectmultiInfo = require('./subpagecontent/login/subpagecontent/selectmultiInfo');
import { loopSession, loopBpSession } from 'clientConfig/util/queryurlfield';
let loginFilter = (callback, props) => {
    var unionid = Storage.get('user.unionid');
    if (unionid) {
        let type = window.type;
        let body = {};
        let name = monStorage.get('user.nickname') || '';
        let email = monStorage.get('user.email') || '';
        let avatar = monStorage.get('user.avatar') || '';
        // let unionid = Storage.get('user.unionid')
        let corp_name = monStorage.get('user.corp_name') || '';
        let auth_level = monStorage.get('user.auth_level') || '';
        let bpPassword = monStorage.get('user.bpPassword') || '';
        let bpPortalID = monStorage.get('user.bpPortalID') || '';
        let bpResourceName = monStorage.get('user.bpResourceName') || '';
        let bpUserGUID = Storage.get('user.unionid') || '';
        let bpUserID = Storage.get('user.nickname') || '';
        if (type == 'bp') {
            body = { bpPassword, bpPortalID, bpResourceName, bpUserGUID, bpUserID }
        } else if(type == 'ding') {
            body = { name, email, avatar, unionid, corp_name, auth_level };
        }
        let url = urllist.getInfoById + '?type=' + type + '&id=' + unionid;
        net.fetchUrl(url, { method: "post", body: { ...body } }).then((result) => {
            if (!result) return;
            // console.log("result=" + JSON.stringify(result))          
            loopSession();
            //   alert(JSON.stringify(result));
            //4117-未注册的ding 成功-0
            // callback();
            if (result.errcode == 0) {
                monStorage.set('user.session', result.session);
                Storage.set('user.modified', result.user.modified);
                Storage.set('user.id', result.user.id);
                Storage.set('user.info', result);
                return result;
                // Control.go('/home', result)

            } else if (result.errcode == 4116) {
                Toast.show({
                    type: 'error',
                    content: '暂不支持该平台',
                    duration: 3000
                })
            } else if (result.errcode == 4117) {
                callback();
            } else if (result.errcode == 4118) {
                console.log("用户第三方信息未添加")
            }
        }).then((result) => {
            if (result) {
                let url = urllist.is_bind + '?id=' + Storage.get('user.id');
                net.fetchUrl(url).then((jsondata) => {
                    if (jsondata.errcode == 0) {
                        // debugger
                        result.role = { text: jsondata.bind_info };
                        Control.go('/home', {result})
                    } else {
                        Control.go('/bind/userbind', result);
                    }
                });
            }
        });
    } else {
        Toast.show({
            type: 'error',
            content: '免登用户id出错'
        });
    }

    // 
};
const PageBind = {
    page: require('./PageBind'),
    route: () => {
        return (<div>
            <Route path='/bind' component={Login} />
        </div>)
    }

}
const Login = () => {
    return (
        <div>
            <Route component={PageBind.page} path='/index' enterFilter={loginFilter}></Route>
            <Route component={Modify} path='/modify'></Route>
            <Route name="用户二次绑定页面" component={PageUserbind} path='/userbind'></Route>
        </div>)
}
const Modify = () => {
    return (
        <div>
            <Route name='资料填充页' component={PageLogin} path='/fill' ></Route>
            <Route name='输入' component={PageFillInfo} path='/fillInfo(/:id)(/:type)'></Route>
            <Route name='单选框' component={PageSelectInfo} path='/selectInfo(/:id)(/:city)(/:type)'></Route>
            <Route name='多页单选框' component={PageSelectmultiInfo} path='/selectmultiInfo(/:type)'></Route>
        </div>)
}
export default PageBind;